package darkwindclient.gui;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * @author Jacob Ferm
 */
public class TextField extends JTextPane {
	private static final long serialVersionUID = 1L;
	private Style style;
	private StyledDocument doc;
	private Color currentColor;

	public TextField() {
		super();

//		setSize(690, 700);
		setOpaque(true);
		setBackground(Color.BLACK);
		setForeground(Color.WHITE);
		setEditable(false);
		setVisible(true);
		setFocusable(false);
		setFont(new Font("Simplified Arabic Fixed", Font.PLAIN, 14));
		doc = getStyledDocument();
		style = addStyle("I'm a Style", null);
		StyleConstants.setForeground(style, Color.WHITE);
		StyleConstants.setBackground(style, Color.BLACK);
	}

	/**
	 * Adds a character using the current style to the textfield.
	 * @param c the character to be added
	 */
	public void print(char c) {
		try {
			doc.insertString(doc.getLength(), "" + c, style);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Adds a string to the textfield using the current style. (Should be changed to a specific style)
	 * @param s the string to be added
	 */
	public void printCommand(String s) {
		try {
			doc.insertString(doc.getLength(), s + '\n', style);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Updates the current style.
	 * @param colorCode id for the new style
	 */
	public void updateStyle(int colorCode) {
		switch (colorCode) {
		case 0:
			currentColor = Color.WHITE;
			StyleConstants.setForeground(style, currentColor.darker());
			StyleConstants.setBackground(style, Color.BLACK.darker());
			StyleConstants.setItalic(style, false);
			StyleConstants.setUnderline(style, false);
			StyleConstants.setStrikeThrough(style, false);
			break;
		case 1:
			StyleConstants.setForeground(style, currentColor.brighter());
			break;
		case 3:
			StyleConstants.setItalic(style, true);
			break;
		case 4:
			StyleConstants.setUnderline(style, true);
			break;
		case 7:
			System.out.println("Inverse style not found");
			break;
		case 9:
			StyleConstants.setStrikeThrough(style, true);
			break;
		case 22:
			StyleConstants.setForeground(style, currentColor.darker());
			break;
		case 23:
			StyleConstants.setItalic(style, false);
			break;
		case 24:
			StyleConstants.setUnderline(style, false);
			break;
		case 27:
			System.out.println("Inverse style not found");
			break;
		case 29:
			StyleConstants.setStrikeThrough(style, false);
			break;
		case 30:
			currentColor = Color.BLACK;
			StyleConstants.setForeground(style, currentColor.darker());
			break;
		case 31:
			currentColor = Color.RED;
			StyleConstants.setForeground(style, currentColor.darker());
			break;
		case 32:
			currentColor = Color.GREEN;
			StyleConstants.setForeground(style, currentColor.darker());
			break;
		case 33:
			currentColor = Color.YELLOW;
			StyleConstants.setForeground(style, currentColor.darker());
			break;
		case 34:
			currentColor = Color.BLUE;
			StyleConstants.setForeground(style, currentColor.darker());
			break;
		case 35:
			currentColor = Color.MAGENTA;
			StyleConstants.setForeground(style, currentColor.darker());
			break;
		case 36:
			currentColor = Color.CYAN;
			StyleConstants.setForeground(style, currentColor.darker());
			break;
		case 37:
			currentColor = Color.WHITE;
			StyleConstants.setForeground(style, currentColor.darker());
			break;	
		case 39:
			currentColor = Color.WHITE;
			StyleConstants.setForeground(style, currentColor.darker());
			break;
		case 40:
			StyleConstants.setBackground(style, Color.BLACK.darker());
			break;
		case 41:
			StyleConstants.setBackground(style, Color.RED.darker());
			break;
		case 42:
			StyleConstants.setBackground(style, Color.GREEN.darker());
			break;
		case 43:
			StyleConstants.setBackground(style, Color.YELLOW.darker());
			break;
		case 44:
			StyleConstants.setBackground(style, Color.BLUE.darker());
			break;
		case 45:
			StyleConstants.setBackground(style, Color.MAGENTA.darker());
			break;
		case 46:
			StyleConstants.setBackground(style, Color.CYAN.darker());
			break;
		case 47:
			StyleConstants.setBackground(style, Color.WHITE.darker());
			break;
		case 49:
			StyleConstants.setBackground(style, Color.WHITE.darker());
			break;
		default:
			System.out.println("Code: " + colorCode + " was not recognized");
		}

	}

}
